package com.achf.siap.cd.dao;

import com.achf.siap.cd.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 * http://www.urubatan.com.br/dao-generico-um-exemplo-a-pedidos/
 * @author Aureliano
 */
public class DAO<T> implements IDAO<T> {

    private final Class<T> entityClass;

    public DAO(Class<T> clazz) {
        this.entityClass = clazz;
    }

    @Override
    public Class<?> getEntityClass() {
        return this.entityClass;
    }

    @Override
    public void save(T entity) {
        Session sessao = HibernateUtil.getSession();
        sessao.save(entity);
        sessao.flush();
    }

    @Override
    public void delete(T entity) {
        Session sessao = HibernateUtil.getSession();
        sessao.delete(entity);
        sessao.flush();
    }

    @Override
    public void refresh(T entity) {
        Session sessao = HibernateUtil.getSession();
        sessao.refresh(entity);
    }

    @Override
    public T load(Long id) {
        Session sessao = HibernateUtil.getSession();
        return (T) sessao.load(this.entityClass, id);
    }

    @Override
    public T get(Long id) {
        Session sessao = HibernateUtil.getSession();
        return (T) sessao.get(this.entityClass, id);
    }

    @Override
    public List<T> listAll() {
        Session sessao = HibernateUtil.getSession();
        Criteria criteria = sessao.createCriteria(this.entityClass);
        this.addOrderToCriteria(criteria);

        return criteria.list();
    }

    @Override
    public List<T> findByExample(T example) {
        Session sessao = HibernateUtil.getSession();
        Criteria criteria = sessao.createCriteria(this.entityClass);
        this.addOrderToCriteria(criteria);
        this.addFilterToCriteria(criteria, example);

        return criteria.list();
    }

    @Override
    public T findOneByExample(T example) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<T> listAll(final int first, final int max) {
        Session sessao = HibernateUtil.getSession();
        Criteria criteria = sessao.createCriteria(this.entityClass);
        this.addOrderToCriteria(criteria);

        if (first > 0) {
            criteria.setFirstResult(first);
        }

        if ((max > 0) && (max > first)) {
            criteria.setMaxResults(max);
        }

        return criteria.list();
    }

    @Override
    public int listAllPageCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<T> findByExample(T example, int first, int max) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int findByExamplePageCount(T example) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected void addOrderToCriteria(final Criteria criteria) {

    }

    protected void addFilterToCriteria(final Criteria criteria, final T example) {
        
    }
}